package com.party.mobile.task;

import com.party.core.model.activity.Activity;
import com.party.core.model.crowdfund.*;
import com.party.core.service.activity.IActivityService;
import com.party.core.service.crowdfund.*;
import com.party.mobile.biz.order.payOrder.PayOrderFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author: huangzequn
 * @Date: 2019/4/8 17:31
 * 定时刷新多场活动众筹阶段
 */
@Component(value = "quartzCrowdfundPhase")
public class QuartzCrowdfundPhase {


    Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private IEventGradationService eventGradationService;

    @Autowired
    private IProjectGradationService projectGradationService;

    @Autowired
    private IProjectInfoGradationService projectInfoGradationService;

    @Autowired
    private ICrowdfundEventService crowdfundEventService;

    @Autowired
    private IProjectService projectService;

    @Autowired
    private PayOrderFactory payOrderFactory;

    @Autowired
    private IActivityService activityService;

    /**
     * 定时刷新多场活动众筹阶段
     */
    @Scheduled(cron="0 0 1 * * ?")
    public void updateCrowdfundPhase() {
        long start, end;
        start = System.currentTimeMillis();
        logger.info("定时刷新多场活动众筹阶段   开始执行");
        /** 找到所有多场没有截止的的活动*/

        List<CrowdfundEvent> crowdfundEventList = crowdfundEventService.getMultipleCrowdfundEvent();
        logger.info("定时刷新多场活动  " + crowdfundEventList.size());
        for (CrowdfundEvent crowdfundEvent : crowdfundEventList) {
            /** 当前系统时间在哪个阶段*/
            EventGradation ongoingGradation = eventGradationService.findOngoingGradation(crowdfundEvent.getId());
            if (null == ongoingGradation) {
                //最后阶段主题
                ongoingGradation = eventGradationService.getTheLastGradation(crowdfundEvent.getId());
            }
            /** 找到这个活动下面的所有报名人员*/
            List<Project> projectList = projectService.findByEventId(crowdfundEvent.getId());
            for (Project project : projectList) {
                try {
                    /** 当前众筹者正在进行哪个阶段  zc_project_info*/
                    ProjectInfoGradation byProjectId = projectInfoGradationService.findByProjectId(project.getId());
                    ProjectGradation projectGradation1 = projectGradationService.get(byProjectId.getProjectGradationId());
                    EventGradation eventGradation1 = eventGradationService.get(projectGradation1.getEventGradationId());
                    /** 如果当前系统的阶段大于zc_project_info表中正在进行的阶段 就修改zc_project_info
                     * 出现这种的情况是：如果众筹者在第一个阶段没有筹满 但是系统时间到了第二阶段
                     */
                    if (ongoingGradation.getGradation() > eventGradation1.getGradation()) {
                        Activity activity = activityService.findByProjectId(project.getId());
                        payOrderFactory.automaticShift(ongoingGradation, project, activity);
                    }
                } catch (Exception e) {
                    logger.error("定时刷新多场活动众筹阶段异常{},众筹id为{}", e, project.getId());
                    continue;
                }
            }
        }
        end = System.currentTimeMillis();
        logger.info("定时刷新多场活动众筹阶段   执行结束====" + (end - start) + "(ms)");
    }
}
